$(function(){
	init1();
	menu.loadIndexMenuApp();
});
function init1(){
	var application=$('#application');
	if(!application.is('div')){
		application=$('<div>').attr('id','application');
		application.append($('<div>').addClass('application'));
		application.appendTo($(document.body));
	}
}
var menu={
	loadIndexMenuApp:function(id){
		var deps=['indexMenuAppTemp'];
		var data;
		if(id){
			data={'t.menuId':id};
		}else{
			data={};
		}
		template.loadDep(deps, {}, function(){
			$.getJSON("userMenuApplicationAction_loadMenuApp.action",data,function(data){
				if($.isEmptyObject(data.actionErrors)){
					if(data.t.menuId && data.t.menuId!='null'){
						var par=undefined;
						if(data.t==null){
							par='null';
						} else{
							par=data.t.parentId;
						}
						var parent={
							menuId:par,
							menuName:'返回'
						};
						data.list.splice(0,0,parent);
					}
					var app=$('#application .application').empty();
					var ul=$('<ul>');
					ul.append($("#indexMenuAppTemp").tmpl(data));
					app.append(ul);
				}else{
					alert(data.actionErrors.join(','));
				}
			});
		});
	},
	listAllMenu:function(option){
		$.getJSON("userMenuApplicationAction_listAllMenu.action",function(data){
			if($.isEmptyObject(data.actionErrors)){
				var select=$(option.menuListTarget).empty();
				var menu={
					menuId:'',
					menuName:'请选择'
				};
				data.list.splice(0,0,menu);
				$.each(data.list,function(i,n){
					select.append($("<option></option>").attr('value',n.menuId).html(n.menuName));
				});
			}else{
				alert(data.actionErrors.join(','));
			}
		});
		
	},
	listAllApplication:function(option){
		$.getJSON("applicationAction_listAll.action",function(data){
			if($.isEmptyObject(data.actionErrors)){
				var select=$(option.applicationListTarget).empty();
				var menu={
					id:'',
					name:'请选择'
				};
				data.list.splice(0,0,menu);
				select.append($("#option").tmpl(data.list));
			}else{
				alert(data.actionErrors.join(','));
			}
		});
	}
	,
	openAddUserMenuApplication:function(){
		commons.openDia({
			'dialog':'userMenuApplicationAdd',
			'callback':[menu.listAllMenu,menu.listAllApplication],
			'menuListTarget':'#userMenuApplicationAdd select[name="t.parentId"]',
			'applicationListTarget':'#userMenuApplicationAdd select[name="t.applicationId"]',
			'dep':'option'
		});
	},
	openUserMenuApplicationList:function(){
		commons.openDia({
			'dialog':'userMenuApplicationSearchList',
			'callback':[menu.listAllMenu],
			'menuListTarget':'#userMenuApplicationSearchList select[name="t.parentId"]',
			'dep':'option'
		});
	},
	loadApplicationInfo:function(select){
		var form=$(select).parent();
		if(select.value){
			$.getJSON("applicationAction_load.action",{'t.id':select.value},function(data){
				if($.isEmptyObject(data.actionErrors)){
					form.children('input[name="t.menuName"]').val(data.t.name);
					form.children('input[name="t.icon"]').val(data.t.icon);
				}else{
					alert(data.actionErrors.join(','));
				}
			});
		}
	}
};